<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Quick Start</title>
  <script src="/js/spritejs.js"></script>
  <script src="//lib.baomitu.com/d3/4.10.2/d3.js"></script>
  <script src="//d3js.org/topojson.v1.min.js"></script>
  <style>
    html,body {
      margin: 0;
      padding: 0;
      background: #201e1e;
      height: 100%;
      width: 100%;
      max-width: 800px;
    }

    #stage {
      display: inline-block;
      width: 100%;
      height: 0;
      padding-bottom: 100%;
      background: #201e1e;
    }  
  </style>
</head>
<body>
  <div id="stage"></div>
  <script>
    /* globals d3, topojson */
    const {Scene} = spritejs;
    const container = document.getElementById('stage');
    const width = 1000,
      height = 750;

    const scene = new Scene({
      container,
      width,
      height,
      mode: 'stickyWidth',
    });

    let centered;
    const projection = d3.geoAlbersUsa()
      .scale(1070)
      .translate([width / 2, height / 2]);

    const path = d3.geoPath()
      .projection(projection);

    const layer = scene.layer('fglayer', {
      handleEvent: true,
    });

    layer.canvas.style.backgroundColor = '#8DBF59';

    d3.json('https://s4.ssl.qhres.com/static/4e8ebcccf5b5ea78.json', (error, us) => {
      if(error) throw error;

      d3.select(layer).selectAll('path')
        .data(topojson.feature(us, us.objects.states).features)
        .enter()
        .append('path')
        .attr('d', path)
        .attr('strokeColor', '#618F4A')
        .attr('lineWidth', 1)
        .attr('fillColor', '#70A556')
        .on('click', clicked);
    });

    function clicked(d) {
      // If the click was on the centered state or the background, re-center.
      // Otherwise, center the clicked-on state.
      let translate = [0, 0],
        centroid = [0, 0];

      if(d && centered !== d) {
        centroid = path.centroid(d);
        translate = projection.translate();
        centered = d;
      }

      d3.select(layer).selectAll('path')
        .transition()
        .duration(750)
        .attr('pos', [translate[0] - centroid[0],
          translate[1] - centroid[1]]);
    }
  </script>
</body>
</html>